package business;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.NoResultException;
import javax.persistence.Persistence;
import javax.persistence.Query;

public class ObatListing {

    private int jumlahObat = -1;

    public ObatListing() {
        emf = Persistence.createEntityManagerFactory("apoltekkPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    /**
     * @return the jumlahObat
     */
    public int getJumlahObat() {

        if (jumlahObat == -1) {
            EntityManager em = null;
            try {
                em = getEntityManager();
                Query q = em.createQuery("SELECT count(o) FROM obat as o");
                Number jumlah = (Number) q.getSingleResult();
                jumlahObat = jumlah.intValue();

            } catch (javax.persistence.EntityNotFoundException e) {
            } finally {
                if (em != null) {
                    em.close();
                }
            }
        }

        return jumlahObat;
    }

    public List<Obat> obatTerbaru() {
        List<Obat> obat = null;
        EntityManager em = null;
        try {
            em = getEntityManager();
            Query q = em.createQuery("SELECT object(o) FROM obat as o ORDER BY o.kode");
            obat = q.getResultList();

        } catch (javax.persistence.EntityNotFoundException e) {
        } finally {
            if (em != null) {
                em.close();
            }
        }
        return obat;
    }

    public Obat getObat (String merk){
        Obat l  = null;
        EntityManager em = null;
        try {
            em = getEntityManager();
            Query q = em.createQuery("SELECT object(o) FROM obat as o WHERE o.admin = :admin");
            q.setParameter("merk", merk);
            l = (Obat) q.getSingleResult();
        }catch(NoResultException e){

        }finally {
            if (em != null) {
                em.close();
            }
        }

        return l;
    }

    public Obat getObatId (Long kode){
        Obat l  = null;
        EntityManager em = null;
        try {
            em = getEntityManager();
            Query q = em.createQuery("SELECT object(o) FROM obat as o WHERE o.kode = :kode");
            q.setParameter("kode", kode);
            l = (Obat) q.getSingleResult();
        }catch(NoResultException e){

        }finally {
            if (em != null) {
                em.close();
            }
        }

        return l;
    }

    public void tambahObat(Obat obat) {

        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(obat);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }

    }

    public void hapusObat(Obat obat) {

        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(obat);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }

    }

    public void editObat(Obat obat) {

        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(obat);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }

    }
}
